<template>
	<view>
		<view class="search-box">
			<!-- <uni-search-bar @input="input" :bgColor="bgColor" :radius="80" cancelButton="none"></uni-search-bar> -->
			<uni-search-bar @input="input" :bgColor="bgColor" :radius="80" cancelButton="none" @clear="clear"></uni-search-bar>
		</view>
		
		<view class="history-box" v-if="searchList.length == 0">
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="20" @click="clearHistory"></uni-icons>
			</view>
			
			<view class="history-list">
				<uni-tag class="uni-tag" type="error" inverted="true" :text="item" v-for="(item,index) in historyCom" :key='index' @click="goGoodsList(item)"></uni-tag>
			</view>
		</view>
		
		<view class="search-list" v-else>
			<view class="search-item" v-for="(item,index) in searchList" :key='index' @click="goGoodsDetail(item.goods_id)">
				<view class="goods-name">{{item.goods_name}}</view>
				<uni-icons type="right" size="30"></uni-icons>
				
			</view>
		</view>
	</view>
</template>

<script>
	import {searchResult} from '@/api/search.js';
	export default {
		data() {
			return {
				bgColor:'#c00000',
				timer:null,
				keyword:'',
				searchList:[],
				historyList:[]
			};
		},
		onLoad(){
			this.historyList = JSON.parse(uni.getStorageSync('keyword') || '[]');
		},
		computed:{
			historyCom(){
				return [...this.historyList].reverse();
			}
		},
		
		methods:{
			input(e){
				clearTimeout(this.timer);
				this.timer = setTimeout(()=>{
					console.log(e);
					this.keyword = e;
					this.getSearchList();
					this.saveHistoryList();
				},500)
			},
			async getSearchList(){
				if(this.keyword === ''){
					this.searchList = [];
					return
				}
				const {data:res} = await searchResult(this.keyword);
				console.log(res);
				if(res.meta.status !== 200){
					return uni.showToast({
						title:'数据请求失败，请稍后',
						icon:'none'
					})
				};
				this.searchList = res.message;
			},
			goGoodsDetail(goods_id){
				uni.navigateTo({
					url:'/subpkg/goods-detail/goods-detail?goods_id=' + goods_id 
				})
			},
			clear(){
				console.log('是否触发清空');
				this.searchList = [];
			},
			saveHistoryList(){
				const set = new Set(this.historyList);
				set.delete(this.keyword);
				set.add(this.keyword);
				this.historyList = Array.from(set);
				uni.setStorageSync('keyword',JSON.stringify(this.historyList));
			},
			clearHistory(){
				this.historyList = [];
				uni.setStorageSync('keyword','[]');
			},
			goGoodsList(keyword){
					uni.navigateTo({
						url:'/subpkg/goods-list/goods-list?query=' + keyword
					})
			},
		}
	}
</script>

<style lang="scss">
.search-box{
	height: 120rpx;
	background-color: #2C405A;
}

.search-list {
	padding: 0 10px;
	.search-item {
		padding: 10px 0;
		border-bottom: 1px solid #ccc;
		display: flex;
		justify-content: center;
		align-items: center;
		.goods-name {
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
		}
	}
}

.history-box{
	padding: 0 10px;
	.history-title {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 50px;
		border-bottom: 1px solid #ccc;
	}
	.history-list {
		margin-top: 10px;
		display: flex;
		flex-wrap: wrap;
		.uni-tag {
			margin-right: 10px;
			font-size: 30px;
		}
	}
}
</style>
